package com.room.search.web.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.room.search.IntegrationTest;
import com.room.search.domain.RoomListingFeature;
import com.room.search.repository.RoomListingFeatureRepository;
import com.room.search.service.dto.RoomListingFeatureDTO;
import com.room.search.service.mapper.RoomListingFeatureMapper;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import javax.persistence.EntityManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

/**
 * Integration tests for the {@link RoomListingFeatureResource} REST controller.
 */
@IntegrationTest
@AutoConfigureMockMvc
@WithMockUser
class RoomListingFeatureResourceIT {

    private static final String DEFAULT_FEATURE_NAME = "AAAAAAAAAA";
    private static final String UPDATED_FEATURE_NAME = "BBBBBBBBBB";

    private static final String ENTITY_API_URL = "/api/room-listing-features";
    private static final String ENTITY_API_URL_ID = ENTITY_API_URL + "/{id}";

    private static Random random = new Random();
    private static AtomicLong count = new AtomicLong(random.nextInt() + (2 * Integer.MAX_VALUE));

    @Autowired
    private RoomListingFeatureRepository roomListingFeatureRepository;

    @Autowired
    private RoomListingFeatureMapper roomListingFeatureMapper;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restRoomListingFeatureMockMvc;

    private RoomListingFeature roomListingFeature;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static RoomListingFeature createEntity(EntityManager em) {
        RoomListingFeature roomListingFeature = new RoomListingFeature().featureName(DEFAULT_FEATURE_NAME);
        return roomListingFeature;
    }

    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static RoomListingFeature createUpdatedEntity(EntityManager em) {
        RoomListingFeature roomListingFeature = new RoomListingFeature().featureName(UPDATED_FEATURE_NAME);
        return roomListingFeature;
    }

    @BeforeEach
    public void initTest() {
        roomListingFeature = createEntity(em);
    }

    @Test
    @Transactional
    void createRoomListingFeature() throws Exception {
        int databaseSizeBeforeCreate = roomListingFeatureRepository.findAll().size();
        // Create the RoomListingFeature
        RoomListingFeatureDTO roomListingFeatureDTO = roomListingFeatureMapper.toDto(roomListingFeature);
        restRoomListingFeatureMockMvc
            .perform(
                post(ENTITY_API_URL)
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomListingFeatureDTO))
            )
            .andExpect(status().isCreated());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeCreate + 1);
        RoomListingFeature testRoomListingFeature = roomListingFeatureList.get(roomListingFeatureList.size() - 1);
        assertThat(testRoomListingFeature.getFeatureName()).isEqualTo(DEFAULT_FEATURE_NAME);
    }

    @Test
    @Transactional
    void createRoomListingFeatureWithExistingId() throws Exception {
        // Create the RoomListingFeature with an existing ID
        roomListingFeature.setId(1L);
        RoomListingFeatureDTO roomListingFeatureDTO = roomListingFeatureMapper.toDto(roomListingFeature);

        int databaseSizeBeforeCreate = roomListingFeatureRepository.findAll().size();

        // An entity with an existing ID cannot be created, so this API call must fail
        restRoomListingFeatureMockMvc
            .perform(
                post(ENTITY_API_URL)
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomListingFeatureDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeCreate);
    }

    @Test
    @Transactional
    void getAllRoomListingFeatures() throws Exception {
        // Initialize the database
        roomListingFeatureRepository.saveAndFlush(roomListingFeature);

        // Get all the roomListingFeatureList
        restRoomListingFeatureMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(roomListingFeature.getId().intValue())))
            .andExpect(jsonPath("$.[*].featureName").value(hasItem(DEFAULT_FEATURE_NAME)));
    }

    @Test
    @Transactional
    void getRoomListingFeature() throws Exception {
        // Initialize the database
        roomListingFeatureRepository.saveAndFlush(roomListingFeature);

        // Get the roomListingFeature
        restRoomListingFeatureMockMvc
            .perform(get(ENTITY_API_URL_ID, roomListingFeature.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(roomListingFeature.getId().intValue()))
            .andExpect(jsonPath("$.featureName").value(DEFAULT_FEATURE_NAME));
    }

    @Test
    @Transactional
    void getNonExistingRoomListingFeature() throws Exception {
        // Get the roomListingFeature
        restRoomListingFeatureMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    void putNewRoomListingFeature() throws Exception {
        // Initialize the database
        roomListingFeatureRepository.saveAndFlush(roomListingFeature);

        int databaseSizeBeforeUpdate = roomListingFeatureRepository.findAll().size();

        // Update the roomListingFeature
        RoomListingFeature updatedRoomListingFeature = roomListingFeatureRepository.findById(roomListingFeature.getId()).get();
        // Disconnect from session so that the updates on updatedRoomListingFeature are not directly saved in db
        em.detach(updatedRoomListingFeature);
        updatedRoomListingFeature.featureName(UPDATED_FEATURE_NAME);
        RoomListingFeatureDTO roomListingFeatureDTO = roomListingFeatureMapper.toDto(updatedRoomListingFeature);

        restRoomListingFeatureMockMvc
            .perform(
                put(ENTITY_API_URL_ID, roomListingFeatureDTO.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomListingFeatureDTO))
            )
            .andExpect(status().isOk());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeUpdate);
        RoomListingFeature testRoomListingFeature = roomListingFeatureList.get(roomListingFeatureList.size() - 1);
        assertThat(testRoomListingFeature.getFeatureName()).isEqualTo(UPDATED_FEATURE_NAME);
    }

    @Test
    @Transactional
    void putNonExistingRoomListingFeature() throws Exception {
        int databaseSizeBeforeUpdate = roomListingFeatureRepository.findAll().size();
        roomListingFeature.setId(count.incrementAndGet());

        // Create the RoomListingFeature
        RoomListingFeatureDTO roomListingFeatureDTO = roomListingFeatureMapper.toDto(roomListingFeature);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restRoomListingFeatureMockMvc
            .perform(
                put(ENTITY_API_URL_ID, roomListingFeatureDTO.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomListingFeatureDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithIdMismatchRoomListingFeature() throws Exception {
        int databaseSizeBeforeUpdate = roomListingFeatureRepository.findAll().size();
        roomListingFeature.setId(count.incrementAndGet());

        // Create the RoomListingFeature
        RoomListingFeatureDTO roomListingFeatureDTO = roomListingFeatureMapper.toDto(roomListingFeature);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomListingFeatureMockMvc
            .perform(
                put(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomListingFeatureDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithMissingIdPathParamRoomListingFeature() throws Exception {
        int databaseSizeBeforeUpdate = roomListingFeatureRepository.findAll().size();
        roomListingFeature.setId(count.incrementAndGet());

        // Create the RoomListingFeature
        RoomListingFeatureDTO roomListingFeatureDTO = roomListingFeatureMapper.toDto(roomListingFeature);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomListingFeatureMockMvc
            .perform(
                put(ENTITY_API_URL)
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomListingFeatureDTO))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void partialUpdateRoomListingFeatureWithPatch() throws Exception {
        // Initialize the database
        roomListingFeatureRepository.saveAndFlush(roomListingFeature);

        int databaseSizeBeforeUpdate = roomListingFeatureRepository.findAll().size();

        // Update the roomListingFeature using partial update
        RoomListingFeature partialUpdatedRoomListingFeature = new RoomListingFeature();
        partialUpdatedRoomListingFeature.setId(roomListingFeature.getId());

        partialUpdatedRoomListingFeature.featureName(UPDATED_FEATURE_NAME);

        restRoomListingFeatureMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedRoomListingFeature.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRoomListingFeature))
            )
            .andExpect(status().isOk());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeUpdate);
        RoomListingFeature testRoomListingFeature = roomListingFeatureList.get(roomListingFeatureList.size() - 1);
        assertThat(testRoomListingFeature.getFeatureName()).isEqualTo(UPDATED_FEATURE_NAME);
    }

    @Test
    @Transactional
    void fullUpdateRoomListingFeatureWithPatch() throws Exception {
        // Initialize the database
        roomListingFeatureRepository.saveAndFlush(roomListingFeature);

        int databaseSizeBeforeUpdate = roomListingFeatureRepository.findAll().size();

        // Update the roomListingFeature using partial update
        RoomListingFeature partialUpdatedRoomListingFeature = new RoomListingFeature();
        partialUpdatedRoomListingFeature.setId(roomListingFeature.getId());

        partialUpdatedRoomListingFeature.featureName(UPDATED_FEATURE_NAME);

        restRoomListingFeatureMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedRoomListingFeature.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRoomListingFeature))
            )
            .andExpect(status().isOk());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeUpdate);
        RoomListingFeature testRoomListingFeature = roomListingFeatureList.get(roomListingFeatureList.size() - 1);
        assertThat(testRoomListingFeature.getFeatureName()).isEqualTo(UPDATED_FEATURE_NAME);
    }

    @Test
    @Transactional
    void patchNonExistingRoomListingFeature() throws Exception {
        int databaseSizeBeforeUpdate = roomListingFeatureRepository.findAll().size();
        roomListingFeature.setId(count.incrementAndGet());

        // Create the RoomListingFeature
        RoomListingFeatureDTO roomListingFeatureDTO = roomListingFeatureMapper.toDto(roomListingFeature);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restRoomListingFeatureMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, roomListingFeatureDTO.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomListingFeatureDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithIdMismatchRoomListingFeature() throws Exception {
        int databaseSizeBeforeUpdate = roomListingFeatureRepository.findAll().size();
        roomListingFeature.setId(count.incrementAndGet());

        // Create the RoomListingFeature
        RoomListingFeatureDTO roomListingFeatureDTO = roomListingFeatureMapper.toDto(roomListingFeature);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomListingFeatureMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomListingFeatureDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithMissingIdPathParamRoomListingFeature() throws Exception {
        int databaseSizeBeforeUpdate = roomListingFeatureRepository.findAll().size();
        roomListingFeature.setId(count.incrementAndGet());

        // Create the RoomListingFeature
        RoomListingFeatureDTO roomListingFeatureDTO = roomListingFeatureMapper.toDto(roomListingFeature);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomListingFeatureMockMvc
            .perform(
                patch(ENTITY_API_URL)
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomListingFeatureDTO))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the RoomListingFeature in the database
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void deleteRoomListingFeature() throws Exception {
        // Initialize the database
        roomListingFeatureRepository.saveAndFlush(roomListingFeature);

        int databaseSizeBeforeDelete = roomListingFeatureRepository.findAll().size();

        // Delete the roomListingFeature
        restRoomListingFeatureMockMvc
            .perform(delete(ENTITY_API_URL_ID, roomListingFeature.getId()).accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<RoomListingFeature> roomListingFeatureList = roomListingFeatureRepository.findAll();
        assertThat(roomListingFeatureList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
